import 'dart:convert';

import 'package:dartkit/dartkit.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';
import 'package:test/test.dart';

const String _content = "测试想法";

// 以想法为例（结构最简单）
// 测试各种方法是否有效
void main() {
  prepareAndCleanPostgres("table_router_test");

  late final Router router = thoughtTable.toRestfulRouter(
    putModify: (Map<String, dynamic> body, Thought object) {
      object.content = body.getField<String>("content");
    },
  );
  late Handler handler = pipelineLogin.addHandler(router.call);

  Future<Thought> createThought([String content = _content]) async {
    final request = tokenRequest(method: "POST", body: {"content": _content});
    final response = await handler.call(request);
    expect(response.statusCode, 200);
    final bodyStr = await response.readAsString();
    final thought = Thought.fromRaw(jsonDecode(bodyStr));
    return thought;
  }

  Future<List<Thought>> getAll() async {
    final request = tokenRequest();
    final response = await handler.call(request);
    expect(response.statusCode, 200);
    final String bodyStr = await response.readAsString();
    final JsonList bodyList = jsonDecode(bodyStr);
    return bodyList.unzip((raw) => Thought.fromRaw(raw));
  }

  argSecret.value = "pomo_handler_test";
  setUp(() async => await getTestToken());

  test("创建测试", () async {
    // 创建一个新想法，检查返回的数据
    final thought = await createThought();
    expect(thought.content, _content);
    expect(thought.timestamp.isNear(DateTime.now()), isTrue);
    expect(thought.id, 1);
  });

  test("单独查找测试", () async {
    // 之前无法找到想法
    final request1 = tokenRequest(router: "/1");
    final response1 = await handler.call(request1);
    expect(response1.statusCode, 404);

    // 创建1个想法，并重新查找，现在可以找到
    await createThought();

    final request2 = tokenRequest(router: "/1");
    final response2 = await handler.call(request2);
    expect(response2.statusCode, 200);
  });

  test("查找所有测试", () async {
    // 之前找到空
    final thoughts1 = await getAll();
    expect(thoughts1, isEmpty);
    // 连续创建3个想法，查到想法数量为3
    await createThought();
    await createThought();
    await createThought();

    final thoughts2 = await getAll();
    expect(thoughts2, hasLength(3));
  });

  test("更新测试", () async {
    // 创建一个想法，并修改其中的内容
    final thought = await createThought();
    thought.content = "修改";

    final request = tokenRequest(
        method: "PUT", router: "/${thought.id}", body: thought.toJson());
    final response = await handler.call(request);
    expect(response.statusCode, 200);

    // 查找所有想法，并读取第一个，发现已经修改
    List<Thought> thoughts = await getAll();
    expect(thoughts, hasLength(1));
    expect(thoughts.first.content, "修改");
  });

  test("删除测试", () async {
    // 一开始删除，会得到404错误
    final request1 = tokenRequest(method: "DELETE", router: "/1");
    final response1 = await handler.call(request1);
    expect(response1.statusCode, 404);

    // 创建一个想法后，再次删除，可以得到204
    await createThought();
    final request2 = tokenRequest(method: "DELETE", router: "/1");
    final response2 = await handler.call(request2);
    expect(response2.statusCode, 204);

    // 此时查找所有想法，会得到空
    List<Thought> thoughts = await getAll();
    expect(thoughts, isEmpty);
  });
}
